package wpRelationsSandboxLinks;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;
import processing.xml.XMLElement;

public class wCategory extends wArticle{
	ArrayList<wCategory> subCats;
	ArrayList<wCategory> superCats;
	ArrayList<wArticle> pages;
	protected static String QUERY_BASE_2 ="http://en.wikipedia.org/w/api.php?format=xml&action=query&list=categorymembers&cmtype=page|subcat&cmprop=type|title&cmtitle=";  
	protected static String QUERY_BASE_3 = "&cmlimit=500&redirects";
	
	
	public wCategory(PApplet theParent, String theTitle) {
		super(theParent, theTitle);
		// TODO Auto-generated constructor stub
	}
	
	public void init() {
		setSubCatsPages();
		setSuperCats();
	}
	
	
	public void printConnections () {
		System.out.println("printing Supercategories...");
	    
		Iterator itr = superCats.iterator();
	    while(itr.hasNext()) System.out.println(itr.next().toString());
		System.out.println("*********");
		System.out.println("printing Subcategories...");
		
		itr = subCats.iterator();
	    while(itr.hasNext()) System.out.println(itr.next().toString());
		System.out.println("*********");
	    System.out.println("printing Pages...");
		
	    itr = pages.iterator();
	    while(itr.hasNext()) System.out.println(itr.next().toString());
		System.out.println("*********");
	}
	
	private void setSubCatsPages() {
		String url = encodeURL(QUERY_BASE_2+title+QUERY_BASE_3);
	    //XMLElement linksXML = GenerativeDesign.loadXMLAsync(parent, url);
		XMLElement linksXML = new XMLElement(super.parent, url);
	    subCats = new ArrayList();
	    pages = new ArrayList();
	    if (linksXML.hasChildren()) {
	        // get titles of categories
	        XMLElement[] children = linksXML.getChildren("query/categorymembers/cm");
	        for (int i = 1; i < children.length; i++) {
	          String title = children[i].getStringAttribute("title");
	          String type = children[i].getStringAttribute("type");
	          if (type.equalsIgnoreCase("page")) {
	        	  pages.add(new wArticle(parent,title));
	          } else {
		          //if (title.startsWith("Category:")) title = title.substring(9);
	        	  subCats.add(new wCategory(parent,title));  
	          }
	        }
	    }
	}
	
	private void setSuperCats() {
		String url = encodeURL(QUERY_BASE_0+title+QUERY_BASE_1);
	    //XMLElement linksXML = GenerativeDesign.loadXMLAsync(parent, url);
		XMLElement linksXML = new XMLElement(super.parent, url);
	    superCats = new ArrayList();
	    if (linksXML.hasChildren()) {
	        // get titles of categories
	        XMLElement[] children = linksXML.getChildren("query/pages/page/categories/cl");
	        for (int i = 1; i < children.length; i++) {
	          String title = children[i].getStringAttribute("title");
	          //if (title.startsWith("Category:")) title = title.substring(9);
	          superCats.add(new wCategory(parent,title));
	        }
	    }
	}
	
}
